<!--计划产值新增-->
<template>
    <base-breadcrumb :breadcrumb="!$route.query.businessId">
        <div class="page-btn-right-top" v-if="!$route.query.businessId">
            <a-button type="primary" @click="submit('DRAFT')" style="margin-right: 10px" :loading="loading" v-if="!disabled && $route.query.pageType!=='change'">
                保存草稿
            </a-button>
            <a-button type="primary" @click="submit('SUBMITTED')" style="margin-right: 10px" :loading="loading" v-if="!disabled">
                提交
            </a-button>
            <a-button @click="close">
                关闭
            </a-button>
        </div>
        <a-card>
            <title-name title="分公司年度计划产值"></title-name>
            <a-form-model
                    ref="ruleForm"
                    :model="form"
                    :rules="rules"
                    :label-col="{span:8}"
                    :wrapper-col="{span: 14}"
            >
                <a-row>
                    <a-col :span="8">
                        <a-form-model-item prop="year" label="分公司" class="requiredFormItem">
                            <a-input
                                    v-if="disabled || $route.query.pageType==='change' || newDisabled"
                                    :disabled="true"
                                    v-model="form.branchComName"></a-input>
                            <a-select
                                    v-else
                                    v-model="form.branchComId" style="width: 100%" placeholder="请选择"
                                      :disabled="disabled || $route.query.pageType==='change'"
                                      @change="branchChange">
                                <a-select-option :value="item.orgId" v-for="item in branchList" :key="item.orgId">
                                    {{ item.orgName }}
                                </a-select-option>
                            </a-select>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="7">
                        <a-form-model-item label="年度：" prop="year" class="requiredFormItem">
                            <a-date-picker mode="year" placeholder="请选择" v-model="form.year"
                                           format="YYYY"
                                           valueFormat="YYYY"
                                           :open="yearOpen"
                                           :disabled="disabled || $route.query.pageType==='change' || newDisabled"
                                           :disabledDate="disabledDate"
                                           @openChange="openChange"
                                           @panelChange="panelChange"></a-date-picker>
                        </a-form-model-item>
                    </a-col>
                    <a-col :span="9">
                        <a-form-model-item prop="year" label="年度计划产值(万元)：">
                            <a-input v-model="form.yearPlanOutput" disabled placeholder="带出下方项目部之和，不可修改"></a-input>
                        </a-form-model-item>
                    </a-col>
                </a-row>
            </a-form-model>
        </a-card>
        <a-card>
            <title-name title="项目部年度计划产值"></title-name>
            <a-button type="primary" style="margin-bottom: 10px" @click="addColumns" v-if="!disabled">新增</a-button>
            <a-table bordered :columns="columns"
                     :loading="loading1"
                     :data-source="dataSource" :pagination="false" rowKey="rowKey" :scroll="{x:true}">
                <template slot="serial" slot-scope="text,record, index">
                    {{ index+1 }}
                </template>
                <template slot="selectPd" slot-scope="text, record, index">
                    <a-input  :disabled="disabled" v-model="record.projectName" v-if="disabled" :title="record.projectName"></a-input>
                    <a-select :value="record.projectId" style="width: 180px" placeholder="请选择"
                              :disabled="disabled"
                              v-else
                              :class="{'requiredFormItem':!disabled}" @change="handleSelect($event, record, index)">
                        <a-select-option :value="item.projectId" v-for="item in pDlist" :key="item.projectId">
                            {{ item.projectName }}
                        </a-select-option>
                    </a-select>
                </template>
<!--                不采用循环来写 是因为template不支持 需要使用render来写-->
                <template slot="action1" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month1"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action2" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month2"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action3" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month3"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action4" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month4"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action5" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month5"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action6" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month6"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action7" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month7"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action8" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month8"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action9" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month9"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action10" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month10"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action11" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month11"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action12" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month12"
                                   @blur="handleChange($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action14" slot-scope="text,record, index">
                    <a style="color: #FF0000" @click="delColumn(index)" v-if="!disabled">删除</a>
                </template>
            </a-table>
        </a-card>
        <a-card>
            <title-name title="预计新开项目年度计划产值"></title-name>
            <a-button type="primary" style="margin-bottom: 10px" @click="addColumns2" v-if="!disabled">新增</a-button>
            <a-table bordered :columns="columns" :data-source="dataSource2"
                     :loading="loading1"
                     :pagination="false" rowKey="rowKey" :scroll="{x:true}">
                <template slot="serial" slot-scope="text,record, index">
                    {{ index+1 }}
                </template>
                <template slot="selectPd" slot-scope="text, record">
                    <a-input  :disabled="disabled"
                              :title="record.projectName"
                              :class="{'requiredFormItem':!disabled}" v-model="record.projectName" placeholder="请输入"></a-input>
                </template>
                <!--                不采用循环来写 是因为template不支持 需要使用render来写-->
                <template slot="action1" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month1"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action2" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month2"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action3" slot-scope="text, record,index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month3"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action4" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month4"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action5" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month5"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action6" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month6"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action7" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month7"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action8" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month8"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action9" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month9"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action10" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month10"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action11" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month11"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action12" slot-scope="text, record, index">
                    <a-input-number style="width: 100%" :max="9999999999999" :min="0" :precision="2" :class="{requiredFormItem:!disabled}" placeholder="请输入" autocomplete="off"
                                    :disabled="disabled"
                                    v-model="record.month12"
                                   @blur="handleChange2($event, index)"
                    ></a-input-number>
                </template>
                <template slot="action14" slot-scope="text,record">
                    <a style="color: #FF0000" @click="delColumn2" v-if="!disabled">删除</a>
                </template>
            </a-table>
        </a-card>
        <selectApprover
                :business-id="businessId"
                :process-config="setProcessConfig"
                :is-close-upper="false"
                @close="cancel"
        />
    </base-breadcrumb>
</template>

<script>
  import { getCurrentUserBranchCom2, queryProjectTree } from '@/api/commons'
  import { createYearPlan, queryYearBybranchId, yearPlanDetail,editYearPlan
  ,queryProjectOutput} from '@/api/project/productionRecords'
  import moment from 'moment'

  export default {
    name: 'addTargetValue',
    data() {
      return {
        newDisabled: false,
        businessId: "",
        setProcessConfig: null,
        processConfig: null,
        rowKey: 0,
        loading1: false,
        loading: false,
        yearOpen: false,
        form: {
          branchComId: null,
          year: "",
          yearPlanOutput: ""
        },
        rules: {},
        columns: [
          {
            title: '序号',
            dataIndex: 'index',
            scopedSlots: { customRender: 'serial' }
          },
          {
            title: '项目部名称',
            width: 200,
            scopedSlots: { customRender: 'selectPd' }
          },
          {
            title: '计划产值（万元）',
            children:[
              {
                title: '1月',
                dataIndex: 'month1',
                scopedSlots: { customRender: 'action1' }
              },
              {
                title: '2月',
                dataIndex: 'month2',
                scopedSlots: { customRender: 'action2' }
              },
              {
                title: '3月',
                dataIndex: 'month3',
                scopedSlots: { customRender: 'action3' }
              },
              {
                title: '4月',
                dataIndex: 'month4',
                scopedSlots: { customRender: 'action4' }
              },
              {
                title: '5月',
                dataIndex: 'month5',
                scopedSlots: { customRender: 'action5' }
              },
              {
                title: '6月',
                dataIndex: 'month6',
                scopedSlots: { customRender: 'action6' }
              },
              {
                title: '7月',
                dataIndex: 'month7',
                scopedSlots: { customRender: 'action7' }
              },
              {
                title: '8月',
                dataIndex: 'month8',
                scopedSlots: { customRender: 'action8' }
              },
              {
                title: '9月',
                dataIndex: 'month9',
                scopedSlots: { customRender: 'action9' }
              },
              {
                title: '10月',
                dataIndex: 'month10',
                scopedSlots: { customRender: 'action10' }
              },
              {
                title: '11月',
                dataIndex: 'month11',
                scopedSlots: { customRender: 'action11' }
              },
              {
                title: '12月',
                dataIndex: 'month12',
                scopedSlots: { customRender: 'action12' }
              },
              {
                title: '总计',
                dataIndex: 'yearPlanOutput'
              }
            ]
          },
          {
            title: '操作',
            fixed: 'right',
            scopedSlots: { customRender: 'action14' }
          }
        ],
        dataSource: [],
        dataSource2: [],
        id: '',
        disabledYearList: [],
        branchList: [], // 分公司下拉
        pDlist: [], //项目部下拉
        cacheYear: '',
        disabled: false
      }
    },
    async created() {
      this.id = this.$route.query.businessId || this.$route.query.id;
      if(this.$route.query.businessId || this.$route.query.pageType==='look') {
        this.disabled = true
      }
      if(this.id) {
        this.yearPlanDetail()
      }
      this.getAllBranchCompany()
      this.processConfig = await this.$getConfigFn('plan_output_value')
    },
    methods: {
        close() {
            this.$router.replace({
                path:'/project/productionRecord',
                query: {
                    activeKey: this.$route.query.activeKey
                }
            })
        },
      // 计划详情
      yearPlanDetail() {
        let data = {
          id: this.id,
          latestData: true
        }
        if(this.$route.query.versionNumber) {
          data.versionNumber = this.$route.query.versionNumber,
            delete data.latestData
        }
        yearPlanDetail(data).then(async (res)=>{
          const result = res.data;
          this.form.branchComName = result.branchComName
          this.form.branchComId = result.branchComId
          this.form.year = result.year
          this.cacheYear = result.year
          if(Number(result.versionNumber) > 1 || this.$route?.query?.t === 'REJECTED') {
            this.newDisabled = true
          }
          // this.form.yearPlanOutput = result.yearPlanOutput
         const projectList =  await  this.queryProjectOutput(true)
          // console.log(projectList, 'projectList')
          this.queryProjectTree()
          const arr1 = result.planOutputValueDeptList.filter(item => item.projectId)
          const arr2 = result.planOutputValueDeptList.filter(item => !item.projectId)
          const arr = []
          const arr3 = []
          if(this.$route.query.pageType === 'change') {
            projectList.forEach(item => {
              const obj = arr1.find(ele => ele.projectId === item.projectId)
              if (!obj) {
                arr1.unshift(item)
              }
            })
          }
          arr1.forEach(item=>{
            let obj = {
              projectId: item.projectId,
              projectName: item.projectName,
              yearPlanOutput: item.yearPlanOutput,
              rowKey: item.projectId
            }

            for(let i = 1; i < 13; i++) {
              const o = item.planOutputValueDeptMonthList.find(ele=> +ele.month === i)
              if(o) {
                obj['month'+i] = o.planOutput
              } else {
                obj['month'+i] = ''
              }
            }
            arr.push(obj)
          })
          arr2.forEach(item=>{
            this.rowKey++
            let obj = {
              projectName: item.projectName,
              yearPlanOutput: item.yearPlanOutput,
              rowKey: this.rowKey
            }

            for(let i = 1; i < 13; i++) {
              const o = item.planOutputValueDeptMonthList.find(ele=> +ele.month === i)
              if(o) {
                obj['month'+i] = o.planOutput
              } else {
                obj['month'+i] = ''
              }
            }
            arr3.push(obj)
          })
          this.dataSource = arr;
          this.dataSource2 = arr3;
          this.calcTotal();
        })
      },
      //年度组件不生效
      disabledDate(date) {
        const y = moment(date).format("YYYY").toString()
        // console.log(y, 'y', this.disabledYearList)
        return this.disabledYearList.includes(y)
      },
      //查询分公司已有年度
      queryYearBybranchId(){
        queryYearBybranchId(this.form.branchComId).then(res=>{
          this.disabledYearList = res.data
          // console.log(res, '年份')
        })
      },
      // 获取分公司
      getAllBranchCompany() {
          getCurrentUserBranchCom2({ tagCodes: ['BC'] }).then(res => {
          if(!this.id) {
            const tagCode = this.$userInfo().currentPost.code
            if(tagCode === "BC") {
              this.form.branchComId = this.$userInfo().currentPost.id
            } else {
              this.form.branchComId = res.data[0].orgId
            }
            this.queryYearBybranchId()
            this.queryProjectTree()
          }
          this.branchList = res.data
        })
      },
      //年度组件打开
      openChange(status) {
        if (status) {
          this.yearOpen = true
        } else {
          this.yearOpen = false
        }
      },
      //年度组件选择
      panelChange(value) {
        this.yearOpen = false
        this.form.year = value
        const year = moment(this.form.year).format("YYYY")
        const _year = moment(this.cacheYear).format('YYYY')
        if (this.disabledYearList.includes(year.toString()) && (!this.id || this.$route.query.pageType==='edit')&&year!==_year) {
          this.$message.warning("当前分公司年度已有数据")
          this.form.year = ""
          this.dataSource = []
          return
        }
        this.dataSource2 = []
        this.queryProjectOutput()
      },
      //数字输入框变更
      handleChange(val, index) {
        let num = 0;
        let ele = this.dataSource[index]
        for(let i=1; i<13;i++) {
          if(ele['month'+i]) {
            num += ele['month'+i]*100
          }
        }
        this.dataSource[index].yearPlanOutput = num/100
        this.calcTotal()
      },
      //计算总计
      calcTotal() {
        let total = 0
        this.dataSource.forEach(item=>{
          if(item.yearPlanOutput) {
            total += item.yearPlanOutput*100
          }
        })
        this.dataSource2.forEach(item=>{
          if(item.yearPlanOutput) {
            total += item.yearPlanOutput*100
          }
        })
        console.log(total)
        this.form.yearPlanOutput = total/100
      },
      //数字输入框变更
      handleChange2(val, index) {
        let num = 0;
        let ele = this.dataSource2[index]
        for(let i=1; i<13;i++) {
          if(ele['month'+i]) {
            num += ele['month'+i]*100
          }
        }
        this.dataSource2[index].yearPlanOutput = num/100
        this.calcTotal()
      },
      //提交
      submit(type) {
        const arr = []
        let flag = true;
        if(!this.form.year) {
          this.$message.warning('请选择年度')
          flag = false
          return
        }
        const year = moment(this.form.year).format("YYYY")
        let pIds = []
        this.dataSource.forEach(item=>{
          if(!item.projectId) {
            flag = false
          }
          if(!pIds.includes(item.projectId)) {
            pIds.push(item.projectId)
          }
          let planOutputValueDeptMonthList = []
          for (let i=1; i<13; i++) {
            let obj = {
              month: i,
              monthly: `${year}-${i < 10 ? '0'+i : i}-01 01:00:00`,
              planOutput: item['month'+i]
            }
            planOutputValueDeptMonthList.push(obj)
            if(item['month'+i] === '' || item['month'+i] === null) {
              flag = false
            }
          }
          let o = {
            planOutputValueDeptMonthList,
            projectId: item.projectId,
            yearPlanOutput: item.yearPlanOutput
          }
          arr.push(o)
        })
        this.dataSource2.forEach(item=>{
          if(!item.projectName) {
            flag = false
          }
          let planOutputValueDeptMonthList = []
          for (let i=1; i<13; i++) {
            let obj = {
              month: i,
              monthly: `${year}-${i < 10 ? '0'+i : i}-01 01:00:00`,
              planOutput: item['month'+i]
            }
            planOutputValueDeptMonthList.push(obj)
            if(item['month'+i] === '' || item['month'+i] === null) {
              flag = false
            }
          }
          let o = {
            planOutputValueDeptMonthList,
            projectName: item.projectName,
            yearPlanOutput: item.yearPlanOutput
          }
          arr.push(o)
        })
        let data = {
          ...this.form,
          year: moment(this.form.year).format("YYYY-MM-DD hh:mm:ss"),
          planOutputValueDeptList:arr,
          auditStatus: type,
          isChange: this.$route.query.pageType === 'change' ? true : false
        }
        const submit = ()=>{
          if (!this.id) {
            // 新增
            createYearPlan(data).then(res => {
              this.loading = false
              if(res.code === 200) {
                this.$message.success(type === 'DRAFT' ? '保存成功':'提交成功')
                if(type==='SUBMITTED') {
                  this.businessId = res.data.id;
                  this.setProcessConfig = this.processConfig;
                  if(this.processConfig===4) {
                      this.close()
                  }
                } else {
                    this.close()
                }
              }
            }).catch(()=>{
              this.loading = false
            })
          } else {
            data.id = this.id;
            editYearPlan(data).then(res => {
              this.loading = false
              if(res.code === 200) {
                this.$message.success(type === 'DRAFT' ? '保存成功':'提交成功')
                if(type==='SUBMITTED') {
                  this.businessId = this.id;
                  this.setProcessConfig = this.processConfig;
                  if(this.processConfig===4) {
                    this.$router.back()
                  }
                } else {
                  this.$router.back()
                }
              }
            }).catch(()=>{
              this.loading = false
            })
          }
        }
        if(type === 'DRAFT') {
          this.loading = true
          submit()
          return
        }
        const _year = moment(this.cacheYear).format('YYYY')
        if (this.disabledYearList.includes(year.toString()) && (!this.id || this.$route.query.pageType==='edit') && year!==_year) {
          this.$message.warning("当前分公司年度已有数据")
          return
        }
        if(pIds.length < this.dataSource.length) {
          this.$message.warning('项目部年度计划产值项目部名称重复')
          this.loading = false
          return
        }
        if(this.dataSource.length===0) {
          this.$message.warning('请添加项目部年度计划产值')
          return
        }
        // if(this.dataSource2.length===0) {
        //   this.$message.warning('请添加预计新开项目年度计划产值')
        //   return
        // }
        if(!flag) {
          this.$message.warning("请填写完所有必填项信息")
          this.loading = false
          return
        }
        this.loading = true
        this.submitApprovalConfirm(4, (flag)=>{
            if(!flag) {
              this.loading = false
              return
            }
            submit()
        }, '请确认是否要提交计划产值？')
      },
      //新增列
      addColumns() {
        if(!this.form.year) {
          this.$message.warning('请先选择年度')
          return
        }
        this.rowKey++
        let obj = {
          projectId: '',
          yearPlanOutput: '',
          rowKey: this.rowKey
        }
        for(let i = 1; i < 13; i++) {
          obj['month'+i] = ''
        }
        this.dataSource.unshift(obj)
      },
      addColumns2() {
        this.rowKey++
        let obj = {
          projectName: '',
          yearPlanOutput: '',
          rowKey:this.rowKey
        }
        for(let i = 1; i < 13; i++) {
          obj['month'+i] = ''
        }
        this.dataSource2.unshift(obj)
      },
      //删除列
      delColumn(i) {
        let vm = this
        this.$confirm({
          title: '请确认是否删除该条数据？',
          onOk: () => {
            vm.dataSource.splice(i,1)
            vm.calcTotal()
          }
        })
      },
      //删除列
      delColumn2(i) {
        let vm = this
        this.$confirm({
          title: '请确认是否删除该条数据？',
          onOk: () => {
            vm.dataSource2.splice(i,1)
            vm.calcTotal()
          }
        })
      },
      //分公司选择
      branchChange() {
        this.dataSource = []
        this.dataSource2 = []
        this.queryYearBybranchId()
        this.queryProjectOutput()
        this.queryProjectTree()
      },
      //查询项目产值记录
    async queryProjectOutput(flag=false) {
        if(!this.form.branchComId || !this.form.year) {
          return
        }
        const year = moment(this.form.year).format("YYYY")
        const data = {
          branchComId: this.form.branchComId,
          businessType: 1,
          year
        }
        this.loading1 = true
        let res = await queryProjectOutput(data)
          // this.pDlist = res.data;
          if(flag) {
            this.loading1 = false
            return res.data || []
          }
          const arr = []
          res.data.forEach(item=>{
            //累加 统计
            let obj = {
              projectId: item.projectId,
              yearPlanOutput: item.yearPlanOutput,
              rowKey: item.projectId
            }

            for(let i = 1; i < 13; i++) {
             const o = item.planOutputValueDeptMonthList.find(ele=> +ele.month === i)
              if(o) {
                obj['month'+i] = o.planOutput
              } else {
                obj['month'+i] = ''
              }
            }
            arr.push(obj)
          })
          this.dataSource = arr;
          this.calcTotal();
          this.loading1 = false
      },
      //项目选择
      handleSelect(val, record, index){
       const idx = this.dataSource.findIndex((item)=> item.projectId === val)
        if(idx > -1) {
          this.$message.warning('项目部年度计划产值,同一个项目不能重复添加')
          this.dataSource[index].projectId = ""
          return
        }
        this.dataSource[index].projectId = val
      },
      cancel() {
        this.$router.back()
      },
      //查询项目
      queryProjectTree() {
        let data = {
          orgId: this.form.branchComId,
          statusSet: [1,2,3,4,5,6]
        }
        queryProjectTree(data).then(res=>{
          this.pDlist = []
          this.generateList([res.data])
        })
      },
      /**
       * 获取项目部
       * */
      generateList(data) {
        const newData = JSON.parse(JSON.stringify(data))
        for (let i = 0; i < newData.length; i++) {
          const node = newData[i]
          if(node.tageCode === 'PD') {
            this.pDlist.push(node)
          }
          if (node.childTree) {
            this.generateList(node.childTree)
          }
        }
      },
    }
  }
</script>

<style scoped lang="less">
  /deep/  .requiredFormItem.ant-input-number {
        border: 0!important;
    }
  /deep/  .ant-input-number-handler-wrap {
        display: none!important;
    }
  /deep/ .requiredFormItem.ant-select {
      .ant-select-selection {
          border-color: #e4ac0b;
          background-color: #FFF9E8;
      }
  }
  /deep/ .requiredFormItem.ant-input {
          border-color: #e4ac0b;
          background-color: #FFF9E8;
  }
</style>